package org.thotheolh.sweatkeeper.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.thotheolh.sweatkeeper.helper.Equipment;
import org.thotheolh.sweatkeeper.util.Resource;

public class EditEquipmentDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField equipNameTf;
	private JTextArea commentsTa;
	private int eqid = 0;
	private Equipment eq = null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			NewEquipmentDialog dialog = new NewEquipmentDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public EditEquipmentDialog(int eqid) {
		this.eqid = eqid;
		setTitle("Edit Equipment");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[][grow]", "[][grow]"));
		{
			JLabel equipNameLbl = new JLabel("Equipment Name: ");
			contentPanel.add(equipNameLbl, "cell 0 0,alignx trailing");
		}
		{
			equipNameTf = new JTextField();
			contentPanel.add(equipNameTf, "cell 1 0");
			equipNameTf.setColumns(20);
		}
		{
			JLabel lblComments = new JLabel("Comments: ");
			contentPanel.add(lblComments, "cell 0 1,alignx right,aligny top");
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			contentPanel.add(scrollPane, "cell 1 1,grow");
			{
				commentsTa = new JTextArea();
				commentsTa.setWrapStyleWord(true);
				commentsTa.setLineWrap(true);
				scrollPane.setViewportView(commentsTa);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton cancelBtn = new JButton("Cancel");
				cancelBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						cancel();
					}
				});
				buttonPane.add(cancelBtn);
			}
			{
				JButton delBtn = new JButton("Delete Equipment");
				delBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						delete();
					}
				});
				buttonPane.add(delBtn);
			}
			{
				JButton okBtn = new JButton("Save Edit");
				okBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						save();
					}
				});
				buttonPane.add(okBtn);
				getRootPane().setDefaultButton(okBtn);
			}
		}
		
		initData();
	}
	
	public void initData(){
		eq = Resource.getInstance().getSql().getEquipment(eqid);
		equipNameTf.setText(eq.getEqName());
		commentsTa.setText(eq.getEqComments());
	}
	
	public void cancel(){
		this.dispose();
	}
	
	public void delete(){
		int num = JOptionPane.showConfirmDialog(null, "Do you really want to delete " + eq.getEqName() + " ?", "Delete Equipment ?", JOptionPane.YES_NO_OPTION);
		if (num == JOptionPane.YES_OPTION){
			boolean deleted = Resource.getInstance().getSql().rmEquipment(eqid);
			if (!deleted){
				JOptionPane.showMessageDialog(null, eq.getEqName() + " have failed to be deleted !", "Delete Equipment Failed", JOptionPane.ERROR_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(null, eq.getEqName() + " have successfully been deleted !", "Delete Equipment Succeeded", JOptionPane.INFORMATION_MESSAGE);
			}
			Resource.getInstance().callRefreshable();
			cancel();
		}	
	}
	
	public void save(){
		if (equipNameTf.getText().isEmpty()){
			equipNameTf.setText(eq.getEqName());
		}
		eq.setEqName(equipNameTf.getText());
		eq.setEqComments(commentsTa.getText());		
		boolean stats = Resource.getInstance().getSql().editEquipment(eq);
		if (!stats){
			JOptionPane.showMessageDialog(null, eq.getEqName() + " have failed to be updated !", "Update Equipment Failed", JOptionPane.ERROR_MESSAGE);
		} else {
			JOptionPane.showMessageDialog(null, eq.getEqName() + " have successfully been updated !", "Update Equipment Succeeded", JOptionPane.INFORMATION_MESSAGE);
		}
		Resource.getInstance().callRefreshable();
		cancel();
	}

}
